package com.whut.transportation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.whut.transportation.adapter.SearchTransinfoAdapter;
import com.whut.transportation.adapter.TransinfoAdapter;
import com.whut.transportation.adapter.TransinfoAdapter.MyClickListener;
import com.whut.transportation.entity.AllTransInfo;
import com.whut.transportation.entity.TransInfo;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.utils.Constant;
/**
 * 查询运输信息
 * @author yuliyan
 */
public class SearchTransportationActivity extends BaseActivity{

	private ListView transinfo;
	private List<TransInfo> transinfos;
	private SearchTransinfoAdapter transinfoAdapter;
	private int curpage=1;
	
	private SharedPreferences preferences;
	private Intent intent;
	
	
	/**
	 * 进入页面前加载数据
	 */
	@Override
	protected void onResume() {
		if (transinfoAdapter != null) {
			curpage = 1;
			transinfos.clear();
			transinfoAdapter.notifyDataSetChanged();
			getData(false);
		} else {
			intent = getIntent();
			getData(false);
		}
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add_more:
			getData(false);
			break;
		default:
			break;
		}
		super.onClick(v);
	}
	
	@Override
	protected void findViewById() {
		transinfo = (ListView) findViewById(R.id.transinfo);
	}

	@Override
	protected void afterFindViewId() {
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.activity_search_tranportation);
		setTitleBarView(true, "搜索", -1, true);
		preferences = getSharedPreferences("userId", Context.MODE_PRIVATE);
	}
	@Override
	protected void processLogic() {
		transinfos = new ArrayList<TransInfo>();
	}
	@Override
	protected void setListener() {
	}

	@Override
	protected void getDataAgain() {
		
	}
	//获取数据
	private void getData(final boolean isRefresh) {
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.FIND_SEARCH_TRANSPORTION_INFO;
		vo.context = mContext;
		
		HashMap<String, String> req = new HashMap<String, String>();
		req.put("account", preferences.getString("account", null));
		req.put("name", preferences.getString("name", null));
		req.put("quming", intent.getStringExtra("quming"));
		req.put("searchCon", intent.getStringExtra("searchCon"));
		req.put("searchType", intent.getStringExtra("searchType"));
		vo.requestDataMap = req;
		vo.parser = new JSONParser(AllTransInfo.class);
		getDataFromServer(HttpManager.POST_MOTHOD, vo, 
				new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
							showProgressDialog(getResources().getString(
								R.string.loading_content));
					}
					@Override
					public void onFailed() {
						showNetErrorDialog();
					}
					@Override
					public void processData(
							HashMap<String, Object> paramObject,
							boolean paramBoolean) {
						int code = (Integer)paramObject.get("code");
						if(code == 1){
							AllTransInfo allTransInfo = (AllTransInfo) paramObject.get("result");
							if(isRefresh)
								transinfos.clear();
							transinfos.addAll(allTransInfo.ListTransportInfo);
							if(transinfo.getAdapter()==null){
								transinfoAdapter = new SearchTransinfoAdapter(mContext, transinfos);
								transinfo.setAdapter(transinfoAdapter);
							} else {
								transinfoAdapter.notifyDataSetChanged();
							}
						} else {
							Toast.makeText(mContext, "目前数据为空", Toast.LENGTH_LONG).show();
						}
					}

					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}

}
